package servlet;

import bean.HouseInfo;
import utils.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/list")
public class ListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ArrayList<HouseInfo> list = new ArrayList<>();
        //1.编写sql语句
        String sql = "select * from house_info i,house_type t where i.house_type_id=t.id";
        //2.调用工具类得到结果集
        ResultSet rs = DBUtil.query(sql);
        //3.通过结果集循环得到数据
        try {
            while (rs.next()) {
                int id = rs.getInt(1);
                String leaseMode = rs.getString(2);
                double rent = rs.getDouble(3);
                String contacts = rs.getString(4);
                String depositMethod = rs.getString(5);
                int houseTypeId = rs.getInt(6);
                String address = rs.getString(7);
                String type = rs.getString(9);
                //4.将数据封装成对象存入集合
                HouseInfo house = new HouseInfo(id, leaseMode, rent, contacts, depositMethod, houseTypeId, address, type);
                list.add(house);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //5.将集合添加到request域中
        request.setAttribute("list",list);
        //6.将集合转发给jsp显示
        request.getRequestDispatcher("/WEB-INF/list.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
